package com.heliang;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.WindowManager;

import com.example.appxiehui2.R;
import com.heliang.ui.MainActivity;
import com.heliang.util.Constants;

public class StartActivity extends Activity {
    private int time = 1;
    private Thread thread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        initConstant();

        // 开启线程去跳转
        thread = new Thread(updatethread);
        thread.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    /**
     * 初始化全局变量
     */
    public void initConstant() {
        WindowManager windManager = getWindowManager();
        Display display = windManager.getDefaultDisplay();

        Constants.SCREEN_WIDTH = display.getWidth();
        Constants.SCREEN_HEIGHT = display.getHeight();
        Constants.SDCARD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();

        // 初始化网络类型
        // Constants.APP_NET_TYPE = this.getAPNType();

        // 初始化网卡地址
        initMacAddress();

    }

    Handler updateHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            jumpToMainActivity();
        }

    };

    Runnable updatethread = new Runnable() {
        public void run() {
            Looper.prepare();
            Message msg = updateHandler.obtainMessage();
            try {

                Thread.sleep(time * 1000);

            } catch (Exception e) {
                // TODO: handle exception
                // Log.e(Constants.TAG, e.getMessage());
            }
            updateHandler.sendMessage(msg);
        }
    };

    /**
     * 开启一个线程去初始化网卡地址
     */
    public void initMacAddress() {

    }

    /**
     * @Description: 判断当前网络类型
     * @param: @return
     * @return: int -1：没有网络 1：WIFI网络2：wap网络3：net网络
     * @throws
     * @author heliang
     * @Date 2013-10-7 下午2:36:17
     */
    public int getAPNType() {
        int netType = Constants.NO_NET;
        ConnectivityManager connMgr = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }

        int nType = networkInfo.getType();

        if (nType == ConnectivityManager.TYPE_MOBILE) {
            Log.e("networkInfo.getExtraInfo()",
                "networkInfo.getExtraInfo() is " + networkInfo.getExtraInfo());
            if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
                netType = Constants.CMNET;
            } else {
                netType = Constants.CMWAP;
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = Constants.WIFI;
        }

        return netType;
    }

    /**
     * 跳转
     */
    private void jumpToMainActivity() {
        Intent intent = new Intent(StartActivity.this, MainActivity.class);
        StartActivity.this.startActivityForResult(intent, 0);
        StartActivity.this.finish();
        int version = Integer.valueOf(android.os.Build.VERSION.SDK);
        if (version > 5) {
            overridePendingTransition(R.anim.push_right_in, R.anim.push_left_out);
        }
        StartActivity.this.finish();
    }

}
